<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\View;
use think\Cookie;
use think\Model;
use app\index\model\PromotionModel;
use app\index\model\IndexModel;
class Promotion extends Controller{
	public function index(){
		
	}
	public function snlist(){
		$view=new View();
		$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
		
		$p=new PromotionModel;
		$snlist=$p->getSnList();
		$sntype=$p->getSntypeList();
		$view->assign("list",$snlist);
		$view->assign("sntype",$sntype);
		return $view->fetch();
	}
	
	public function snadd(){
		$view=new View();
		$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
		
		return $view->fetch();
	}
	public function snexport(){
		$view=new View();
		$p=new PromotionModel;
		$type=$_POST['type'];
		if(empty($type)){
			$this->error("SN码不能为空");exit;
		}
		$tname=$p->getSntype($type);
		header("Content-Type: text/html; charset=utf-8");
		header("Content-type:application/vnd.ms-execl");
		header("Content-Disposition:filename=".$tname."SN列表".date("Y-m-d").".xls");
		//   以下\t代表横向跨越一格，\n 代表跳到下一行，可以根据自己的要求，增加相应的输出相，要和循环中的对应哈
		//字段
		$letterArr=explode(',',strtoupper('a,b'));
		$arr=array(
			array('en'=>'sncode','cn'=>'SN码(中奖号)'),
			array('en'=>'tname','cn'=>'SN类名')
		);


		$i=0;
		$fieldCount=count($arr);
		$s=0;
		//thead
		foreach ($arr as $f){
			if ($s<$fieldCount-1){
				echo iconv('utf-8','gbk',$f['cn'])."\t";
			}else {
				echo iconv('utf-8','gbk',$f['cn'])."\n";
			}
			$s++;
		}
		//
		$sns=Db::name('sn')->where(array("type"=>$type,'status'=>1,'used'=>0))->order('nid ASC')->select();
		if ($sns){
//			if ($sns[0]['token']!=$this->token){
//				exit('no permission');
//			}
			foreach ($sns as $sn){
				$j=1;
					$fieldValue=$sn['sncode'];
					$sntname=iconv('utf-8','gbk',$p->getSntype($sn["type"]));
//					switch ($field['en']){
//						default:
//							break;
//						case 'time':
//						case 'sendtime':
//							if ($fieldValue){
//								$fieldValue=date('Y-m-d H:i:s',$fieldValue);
//							}else {
//								$fieldValue='';
//							}
//							break;
//						case 'wecha_name':
//						case 'prize':
//							$fieldValue=iconv('utf-8','gbk',$fieldValue);
//							break;
//					}
						echo $fieldValue."\t";
						echo $sntname."\n";

					$j++;
			}
		}
		exit();
	}
	//won创建SN码
	public function snadd2(){
		if(empty($_POST['name'])){
			$this->error("内容不能为空");exit;
		}
		$m=new PromotionModel;
			$add_time=time();
			$data=array(
				"name" => $_POST["name"],
				"limt" => $_POST["limt"],
				"long" => $_POST["long"],
				"take" => $_POST["take"],
				"snstart" => $_POST["snstart"],
				"snend" => $_POST["snend"],
				"add_time"=>$add_time
			);
			if(empty($data['name']) or empty($data["limt"]) or empty($data["long"]) or empty($data["take"])){
				$this->error('内容必填项一定要填写完整！');
			}
			
			$typeadd=Db::name('sntype')->insert($data);
			$sntype=Db::name('sntype')->where('add_time',$add_time)->find();
			if($typeadd){
				//开始生成SN码
				switch($data["take"]){
					case 1://纯数字
						for($x=1;$x<=$data['limt'];$x++){
							$sncode[$x]=$data['snstart'].strtoupper($m->zm($data["long"],"0123456789")).$data["snend"];
						}
						break;
					case 2://纯字母
						for($x=1;$x<=$data['limt'];$x++){
							$sncode[$x]=$data['snstart'].strtoupper($m->zm($data["long"])).$data["snend"];
						}
						break;
					case 3://混合
						for($x=1;$x<=$data['limt'];$x++){
							$xcode=$m->Keys(1);
							$sncode[$x]=$data['snstart'].mb_substr(strtoupper($xcode[0]), 0, $data["long"], 'utf-8').$data["snend"];
						}
						break;
				}
				$dataSN['type']=$sntype['type'];
				foreach($sncode as $vo){
					$dataSN['sncode']=$vo;
					Db::name('sn')->insert($dataSN);
				}
				
				$this->redirect("Promotion/snshow",array("type"=>$sntype['type']));
			}else{
				$this->error('添加失败');
			}
			
			$this->redirect("Promotion/snshow",array("type"=>$sntype['type']));
		
	}
	public function snshow($type){
		$view=new View();
		$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
		
		$sntype=Db::name('sntype')->where('type',$type)->find();
		$list=Db::name('sn')->where('type',$type)->select();
		$view->assign("list",$list);
		$view->assign("sntype",$sntype);
		return $view->fetch();
	}
	
	public function sncheck(){
		$view=new View();
		$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
		
		$msg="";
		$flag=0;
			if($_POST['sn'] != ''){
				$sn=strtoupper($_POST['sn']);
				$snc=Db::name('sn')->where(array("sncode"=>$sn,"status"=>1))->find();
				$m=new PromotionModel;
				if($snc and $snc['used']==0){
					
					$tname=$m->getSntype($snc["type"]);
					$msg="<h5>您输入的SN码属【".$tname."】</p><p>SN码【".$sn."】已经核销成功！</h5>";
					
					$where['nid']=$snc["nid"];
					$data["nid"]=$snc["nid"];
					$data["used"]=1;
					$data["usetime"]=time();
					Db::name('sn')->where($where)->update($data);
					$flag=1;
				}else{
					if($snc['used']==1){
						$tname=$m->getSntype($snc["type"]);
						$msg="SN码【".$sn."】(".$tname.")于".date("Y-d-m H:i:s",$snc['usetime'])."已经使用！";
					}else{
						$msg="SN码【".$sn."】不存在或已经过期！";
					}
				}
			}else{
				$this->error('SN码不能为空！');
			}

//		dump($update);exit();
		$view->assign('flag',$flag);
		$view->assign('msg',$msg);
		return $view->fetch();
	}
	
	
}
?>